package com.lhy.admin.util;

import com.lhy.admin.Constants;
import com.lhy.admin.common.secure.exception.UnAuthorizedException;
import com.lhy.admin.model.Permission;
import com.lhy.admin.model.Role;
import com.lhy.admin.model.User;
import com.lhy.admin.service.PermissionService;
import com.lhy.admin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class AuthUtil {

    @Autowired
    HttpSession httpSession;

    @Autowired
    RoleService roleService;

    public User getUser() {
        Object attribute = httpSession.getAttribute(Constants.SESSION_USER_KEY);
        if (attribute == null) {
            throw new UnAuthorizedException("Unauthorized");
        }
        return (User)attribute;
    }

    public String getUserName() {
        Object attribute = httpSession.getAttribute(Constants.SESSION_USER_KEY);
        if (attribute == null) {
            throw new UnAuthorizedException("Unauthorized");
        }
        return ((User)attribute).getName();
    }

    public Role getCurrentRole() {
        Object attribute = httpSession.getAttribute(Constants.SESSION_CURRENT_ROLE_KEY);
        if (attribute == null) {
            throw new UnAuthorizedException("Unauthorized");
        }
        return ((Role)attribute);
    }

    public Role setCurrentRole(String roleName) {
        Optional<Role> cond = roleService.getOne(new Role().setName(roleName));
        if (cond.isPresent()) {
            throw new UnAuthorizedException("can not find role："+roleName);
        }
        this.setSessionRoleAndPermission(this.getUser(),cond.get());
        return cond.get();
    }

    public List<String> getPermissionCodes() {
        Object attribute = httpSession.getAttribute(Constants.PERMISSIONS_CODE);
        if (attribute == null) {
            throw new UnAuthorizedException("Unauthorized");
        }
        return ((List<String>)attribute);
    }

    public List<Permission> getPermission() {
        Object attribute = httpSession.getAttribute(Constants.PERMISSIONS);
        if (attribute == null) {
            throw new UnAuthorizedException("Unauthorized");
        }
        return ((List<Permission>)attribute);
    }

    /**
     * @param user
     * @param roles
     * @return void
     * @throws
     * @Title: setSessionRoleAndPermission
     * @Description:  设置第一条role为当前据角色
     * @date 2018/6/19 15:42
     */
    public void setSessionRoleAndPermission(User user, List<Role> roles) {
      this.setSessionRoleAndPermission(user,roles.get(0));
    }

    public void setSessionRoleAndPermission(User user, Role role) {
        httpSession.setAttribute(Constants.SESSION_CURRENT_ROLE_KEY, role);
        //获取权限
        List<Permission> permissions = SpringContextUtil.getBean(PermissionService.class).listPermissionsByUserName(user.getName());
        //设置权限到session
        httpSession.setAttribute(Constants.PERMISSIONS, permissions);
        List<String> permissionCodes = permissions.stream().map(Permission::getCode).collect(Collectors.toList());
        //设置权限CODE到session
        httpSession.setAttribute(Constants.PERMISSIONS_CODE, permissionCodes);
    }

}
